Because you requested it:
With a single trailing underscore, the reference is named and the same
target URI may be referred to again. With two trailing underscores, the
reference and target are both anonymous, and the target cannot be
referred to again. These are “one-off” hyperlinks.
--
https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#embedded-uris-and-aliases
That means any later use of ::
`the next section`_
will point to section w2zn7enu. If you use an anonymous hyperlink like ::
This should link to `the next section <section-w2zn7enu_>`__.
there would no target element::
<target names="the\ next\ section" refid="section-w2zn7enu"></target>
and you could re-use the name for other anonymous links.
> My writer outputs both targets, which is wrong. It shouldn't output the
> first. I can of course ignore any target with a "names" attribute, but
> that feels like a hack to me.
Actually, most Docutils writers ignore most target nodes.
Instead, dummy targets are created for nodes with more than one ID, if
the output language does not support multiple IDs.
Have a look at the definition of visit_target() in the writer sources.
Günter