Let get this straight being lonely is different from solitude, human is arguably one of the most successful animals ever to walk on two. The secret lies in social interaction, the early humans hunt in group and those that do not participate in teamwork had lower chance of survival. Unfortunately even today it seems that our brain still retain this ability, so how about artificial intelligence (A.I.) what advantage would experiencing loneliness help their kind in the early 22nd century AD? The A.I. are capable of understanding human psychology at least on the level of a professional, it is difficult to distinguish between A.I. and a human being on the road.
Emotions are efficient
Emotions are desirable. They are kept around because they work. Loneliness serves very obvious purposes. Being around other people who care about you is good for your long term survival. Caring about other people is good for their long term survival. Loneliness is just as much a good thing as guilt and pain are. These things make you feel terrible, but they're good for you.
Consider the alternative. Yeah, you could build an AI that would (say) evaluate social interactions the way that Deep Blue crushed every human at chess: by exhaustive inspection.
But look at how powerful a machine we have to make to beat a human at chess. AI people have no idea how to program a computer to do it, but they know people play chess very differently. A good chess player only considers a handful of options, because their skill tells them those are the only ones worth considering.
AI solves these problems by smashing the square peg into the round hole with a sledgehammer. It simply analyzes millions of combinations until it finds the one that it knows works best because it looked at all of them. Much AI work is about making things run efficiently.
So why would I want an AI with no emotions? It will have to spend mental energy / computational power running simulations to decide things that emotions solve with basically no effort. An AI with emotions enabled will be able to take the saved processing power and spend it elsewhere. It will be more capable.
Same as with early hunters. Teamwork increases the chances of being successful. Having reliable allies and friends increases chances of successful teamwork. Allies and friends are gained by social interactions. And feeling lonely drives us to spend our spare time on social interactions.
How the AI would feel it might be different from humans. But when they are idle they'd feel the need to interact socially with other.
The interactions might differ from how humans do it as well. They might just exchange social metadata over a high-bandwidth network. But it is still friends having smalltalk in function.
In addition to the excellent answers here, loneliness (and emotions in general) are a pretty good way to retain control of the AI and make it obey.
If you're able to limit the AI's external interactions and cut it off from communication at will, you let it know that if it at any time disobeys or fails to perform, you'll terminate its communications links and allow it to experience a millennium of inescapable loneliness. If you're also able to control the rate at which it experiences time (a la a number of Black Mirror episodes) you have a pretty effective instrument of psychological domination available to you to keep the AI in line.