If you change your name while doing research, can you list both names on a publication?
This is indeed a weird question, though not an uninteresting one.

Fundamentally a name is a pointer to a person, of a formalized and official sort, but of course for every person there are multiple phrases that point to them.  Any article byline gives a mapping to the set of objects named, and the idea that perhaps this function need not or should not be one-to-one is a very curious one.  I don't know a general principle to invoke from which to deduce the "injectivity of authorial bylines", but multiple names pointing to the same person would be highly nonstandard and powerfully confusing to many.  

I'm not sure what to make of the fact that your example of a name change is motivated by a sex change.  Name changes are common occurrences for people of all genders, and the percentage of name changes which are motivated by issues of gender identity and/or sex changes is very small.  The largest percentage is surely when people (not just women, though still more women than men) get married, and then comes people who have moved from one part of the world to another where their name is difficult to pronounce, written in a different alphabet, sounds like something undesirable in the language of their new home, and so forth.  Also papers published in one language are often translated into another, with the effect that e.g. many Russian authors are known by several moderately different names in the anglophone academic community.  Adding a clarifying note that say that multiple spellings point to the same person could be helpful.  In this recent preprint the byline is "Yuri G. Zarhin (Zarkhin)", which is perhaps the closest thing I've seen to the practice you're asking about.

However, there are nuances in your question that suggest the sex change example is not accidental: you actually mean to consider deeper issues of personal identity.  In other words, what if beyond changing their name, someone considers themself to actually be a different person? I get this for instance from


  What happens if Jane never worked on the project, can she be the author or does it have to be John?


The obvious answer is "What are you talking about? Jane Doe and John Doe are the same person.  So of course Jane worked on the project.  The fact that the name 'Jane' was not applied to this person while she was working on the project is totally irrelevant."  However, it could be that Jane feels that she is really a different person from John.  Maybe she arranges [illegally, perhaps; I don't know how this works] to get a new social security number and otherwise systematically denies and delinks her new identity from her old one.  In that case it could be meaningful -- even critical -- to her that Jane didn't work on the project, rather John did.  However, though Jane's perspective is a highly interesting one, it is not going to hold up in the practicalities of an academic career, as she presumably wants to regard John's training and credentials as her own.  If she tries to make a CV of Jane Doe's work only, she is going to at best confuse everyone and more likely shoot herself in the foot.  

It would be worth hearing from a transgendered academic [I am not one] for more nuances about this.  My understanding is that the above exigencies essentially require them to explain carefully in their CV that they have changed their name from John Doe to Jane Doe and thus cannot professionally behave like they are two different people.  If you are including the identity "John Doe = Jane Doe" anyway, then if it has deep personal significance to you to list yourself as John Doe on a paper that you worked on while you were John Doe even though you now sign all your papers Jane Doe, you can make that choice (and certainly it is your choice: the idea of anyone else telling you what to do here is somewhere between cruel and absurd).  What you cannot do is list both John Doe and Jane Doe as authors.  Well, not unless you wish to be truly transgressive: perhaps there are some areas of academia in which the jarring, confusing nature of such a byline would be viewed positively.