You can normally use double quoted strings for this purpose. They accept a range of backslash escape sequences to produce special characters without having to input the special character literals into your buffer.
The character that shows as
^M is the carriage return and you can enter it as
"\r". You can also use key names in double quoted backspace sequences. The key notation for carriage return is
<CR>, so you can produce it in a string with
"\<CR>" are equivalent and produce the same string.
:help expr-quote for a full list of backslash escape sequences that are recognized in a double quoted string.
In your specific case, you could rewrite your function as:
return substitute(a:string, "\r$", '', '')
This will hopefully avoid the conflict with vimlsp that you found with the special character literal.