What is the meaning of 'dabei' in these sentences?
"Er war in seiner Küche dabei, den Geburtstagskuchen anzurühren,"
"Ich wäre gerne dabei gewesen," (Duolingo translates this sentence into: "I should have been there,". But if it's there, why isn't it dort or da instead?).
In the first sentence, "dabei" means "at it/in the process of doing...", it is one way to produce the same effect as the progressive forms in English.
In the second sentence, you see the verb "dabei sein" which means depending on context "to participate/attend/to be in" it refers to something mentioned before. So here the context is maybe a concert somebody tells you about and you answer how much you would have liked to be there but your concern is not the place (then "da/dort" might be better) but the event that you could not attend.
Look here for a sharp look at the word "dabei": https://yourdailygerman.com/2017/02/08/dabei-meaning/
I hope I could be of help.
Duolingo's translation of #2 is not so good; better would be "I would have liked to be there" or "I wish I had been there". If you want to say "I should have been there", better would be "Ich hätte dabei sein sollen/müssen".