YAML is a text-only file, meaning it doesn't support special rendering codes. If you want to highlight parts of your code, you can use an editor that is able to automatically highlight YAML syntax, such as VIM, VSCode or any other modern editor.
If you are trying to embbed color codes as a part of a script/playbook/etc, it depends on how that string is used and how it is parsed (but that would be a very uncommon usecase and really not your original question).
How do you intend to use the colored text?
Red Hat
Learning Community
A collaborative learning environment, enabling open source skill development.