My guess, and this is just a guess, is that the behavior started when you upgraded to calibre 5, at which point the python regular expression system changed.
Calibre has *forever* (since at least 2010) replaced sequences of spaces in template results with a single space. I think, but don't know, that python 3 includes non-breaking-spaces in the set of space characters while python 2 did not. I did not test this theory.
You can avoid removing sequences of spaces by using
General Program Mode templates. For example, the template
doesn't remove internal non-breaking spaces. The template
does remove them. The reason: internal spacing in GPM templates is under the control of the template writer while spacing in non-GPM templates can vary depending on template evaluation.
EDIT: Just to avoid some questions: the current behavior won't change back to what it was.