I've used creative regex before to do this, but Doitsu made a plugin called "
AddIDs - Generates incremental ids" that can easily add an incremental ID to some tag, like <h2>. Then it's really easy to regex those into Text between the tags.
Although, being able to reference the filename would be awesome too.