I have found the solution. Its an overloaded function. If you don't pass the file parameter, it will output to a string.
I can write to a file using yaml.dump but how do I write to a string?
How to write to a string instead of a file wth PyYaml