"He cooks with onions and carrots."
Translation:Anapika na vitunguu na karoti.
Yes. You may also say He cooks rice with onions and translated it to Anapika wali kwa vitunguu and it will still be correct. But kwa is more correct when you explain how does he cook? With gas? With electricity? with charcoal? etc.
He cooks rice and onions with gas stove = Anapika wali na vitunguu kwa stovu ya gesi.