In the same way that @chaley you suggested a to_hex(), I will suggest a to_url() which would allow to make calibre scheme more human like (and will avoid any bugs).
Code:
def evaluate(self, formatter, kwargs, mi, locals, val):
from urllib.parse import quote
return quote(val)
Bal masqué
Bal%20masqu%C3%A9
Code:
<a href="calibre://search/_?q=tags:%22=' & to_url(i) & '%22">
<a href="calibre://search/_?q=tags:%22=Bal%20masqu%C3%A9%22">