What is the correct code for turning HTML elements (H1, H2 or Paragraph etc..) into a link and search engines index texts of header and link both?
which one is better:
<a href="#"><h1>heading</h1></a>
or
<h1><a href="#">heading</a></h1>
The element which displays as a block (in this case <h1>) always should surround %inline elements (such as <a>).
Another example <div> should always be outside <span>.
That is why:
<h1><a href="#">heading</a></h1>is correct.
<a href="#"><h1>heading</h1></a>is Wrong.
An even easier way of understanding this is that the following makes sense:
<h1><a href="#1">link</a> and <a href="#2">Another link</a></h1>
It would be highly unusual to try the inverse with multiple <h1>s inside an <a>.
Reference:

Web Hosting
Web Designs
Graphic Design
SEO
Digital Marketing