In modern Twig you can easily use escape
filter.
{{ ' href="%s%s"' | format('/test-route', '#anchor') | escape }}
format
will act like sprintf
replacing the string
escape
will allow you to use whatever character you want.
reference: https://twig.symfony.com/doc/3.x/filters/escape.html