Links with an anchor and no query string are very common (xwiki.org if full of those) and I don't remember ever having this problem.
I tried on 17.10.9, and I still cannot reproduce what is described:
[[Main.WebHome||anchor="toto"]] {{velocity}} $xwiki.getURL('Main.WebHome', 'view', '', 'HTest') {{/velocity}}
produces the following HTML
<p><span class="wikilink"><a href="/xwiki/bin/view/Main/#toto"><span class="wikigeneratedlinkcontent">Main</span></a></span></p><p>/xwiki/bin/view/Main/#HTest</p>
I think the problem is more what you used as example. AFAIR XWikiSyntax has a pretty special way to treating page content.