Obviously, you can't personally consent while you are in a vegetative state.
But, it is conceivable that consent to this prior to death by a suitable informed consent form or a medical power of attorney agent or other legally authorized medical decision-maker (e.g. a court appointed guardian) could permit this to be done.
I've never actually seen this happen and there is no standard form for this kind of consent.
I'm not aware of researchers interested in doing so.
But there is no legal principle that obviously invalidates such an agreement. Indeed, often in a teaching hospital you sign a general consent form upon admission to the hospital that allows the hospital and the medical professionals working there to use your care for educational and research purposes in ways that aren't contrary to your best interests. What you are contemplating would take more informed consent than that, but this practice proves the concept.
Also, in addition to your consent, the people doing the research would need approval from an institutional human subjects research committee before carrying out this arrangement.