Don't take numbers as unit names
Apply unit conversion before extracting names

Hit a bug that considers "5 in km" as "to_measure(5, to_unit("in")) km",
which is obviously a syntax error.
Replacing the unit conversion syntax with method calls before replacing
the units names fixes the error.