If you do a search for say, 'title attribute', you will get quite a few hits. One that you may find useful, IMNSHO, is
HTML Global title Attribute.
The title attribute is not the same as the title tag.
Sigil and calibre's ebook-editor will use the content of the title="<insert text here>" attribute of the h tag when generating the ToC.
In the examples given, you could use regex to extract the text from the h2 and p tags and concatenate them into a title="whatever" as an attribute of the h2 tag.