Though being a hero or villain has little to do with it, one word that comes to mind is 'flaggelant'. Keep in mind that this word not only describes the act of self-punishment, but also describes the 'how' (by means of flogging/whipping).
Depending on context (ie. why does she punish herself), again without much incorporating the hero/villain concept, another applicable word may be'masochist'be 'masochist'.
Sticking with the assumption that the self-punishment is religion or honor based, the "filling the role of both the hero and the villain" part might be better served with a phrase like 'filling the role of both judge and jury'.