Underscore is allowed in the strings to be compared. The rest is just normal TeX text, thus underscored must be escaped there, just like $, & and # etc.

\IfStrEqCase, like most if the macros of the package, is not expandable, and therefore cannot be used in a \write. So you must rewrite your macro to store the outcome in a macro (that you can pass as the second parameter). And then use that in the write.