What was the reason PSOE couldn't or didn't want to form a coalition for six months or so, to the extent that it preferred to have second snap election in a year? I understand that it had at least two potential coalition parters: Unidas Podemos to the left, and Ciudadanos to the right. What were the disagreements that made either coalition impossible?
Lack of tradition
Since the establishment of democracy, the usual situation was a practical bipartidism between PSOE and AP(now PP), where either:
A political party obtains a majority of the Parliament (Congreso de Diputados), which is enough to have its leader elected as PM.
A political party obtains a plurality great enough to reach a majority if a regionalist/nationalist party (CiU, PNV, CC); the regionalist party supports more or less the government agenda in exchange of some demands for its constituencies but does not form part of the government.
In this context, a policy of demonizing the opposite party and avoiding compromise was rather useful, as support could be bought without risking alienating the supporters of a party by accepting some of the opposing party policies.
Of course, if you have demonized the other party during the electoral campaign, then making pacts after it gets way more complicated, because both of the parties will have more trouble explaining to their electorate why they are making pacts with "the enemy". Until now it was not an issue thanks to bipartidism, but this has been altered by the apparition/greater influence of new political parties (Podemos, Cs, and lately Vox) which eroded the traditional parties pluralities enough to make majorities impossible and "pluralities plus regionalist support" not enough.
Leadership of the left
The PSOE has always claimed to be "the left wing party", or at least "the left wing party that has actual chances at winning"1. OTOH, those parties did usually acknowledge that they would not be able to win the elections but that they would force the PSOE into more leftist policies than it was willing to perform on its own.
Giving government responsabilities to more leftist parties risks erodes the PSOE claims against those parties and gives Unidas Podemos an opportunity to make its point.
It is heavily speculated that Pedro Sánchez did not have much interest in a deal and instead he was very comfortable with the idea of repeating what happened in 2015-2016.
Then the PP got a plurality but not enough support, its leader (Mariano Rajoy) did not even try to form a government while Pedro Sánchez did try to get support but failed. Then elections were repeated in 2016, with increased support (but stil not enough) for the PP, and the PSOE abstained in order to allow Mariano Rajoy to be elected PM by a plurality2. The idea was that if elections were repeated yet again the people would support the candidate with more possibilities to form a government.
In this occasion Pedro Sánchez refused a coalition government, he asked for Unidas Podemos support to get elected which would have given him a free hand to pick different allies for each issue3 (pacting with PP, Cs or Unidas Podemos at his convenience) but the sensation was that there was not much interest in the PSOE making concessions. He even had the wildcard of the regionalist parties being opposed to any right wing coalition which would include Vox, making it easier to get its support or at least no opposition from them.
As a matter of fact, now that it seems that this strategy has failed, the declarations from Sánchez about the possibility of a government pact seem to be quite softer than they were after the previous elections and that negotiations with Unidas Podemos have been resumed after they broke down in June.
UPDATE: Less than 48 hours after the voting stations did close, PSOE and Unidas Podemos have announced a preliminary government deal(link in Spanish), including some of the Unidas Podemos requests which were absolutely vetoed in the previous negotiations.
Which proving that to make a deal it is necessary to want to make a deal...
1At every election the PSOE would claim for himself the "useful vote" from the left, claiming that votes to IU or Unidas Podemos would be wasted and would actually benefit right wing parties.
2As a show of how difficult can be to compromise with the other party, Pedro Sánchez was toppled from the PSOE for not voting against Mariano Rajoy's election as PM even when that was the decision taken by the whole of the PSOE's executive.
3In Spain motions of no confidence must be constructive; they do not only vote against the current PM but they also must propose a new PM. The almost impossibility of getting the opposition parties to agree to an alternative candidate makes a PM position quite safe even without a majority in Parliament; in 40 years of democracy only one motion of no confidance has succeeded and it was after the PM was indicted for the corruption of his party.
Spanish political parties are effectively controlled by banks. All of them except Unidas Podemos are / have been financed through bank credits. Banks condone / do not execute the credits, but in exchange gain an enormous power: if one day they would decide to execute those credits, the parties would be ruined.
Corporations also indirectly control parties, but through promise of future employment in their ranks to politicians.
UP, in contrast, have since the beginning been financed by private persons. They want to limit power to banks and corporations. Of course, in turn, banks and corporations do not want that and pressure PSOE not to form government with them.
Then there's the Catalan independent parties. They will only agree to give their support to PSOE in exchange for an agreement that they will be able to legally vote regarding Catalan independence. PSOE does not want either, because most Spanish people don't want to "break Spain" and they know it would be political suicide to agree to such conditions.
The only two options that add up for government are PSOE + UP + Catalans or PSOE + PP. PP is the right wing (not as extreme right as VOX, but close) and many PSOE voters (and PP, for that matter) are against that option as well.
No matter how you look at it, it is difficult to get a viable government.
PS: I am aware that I'm over-simplyfing when explaining the power that banks and corporations have over parties such as PSOE. It helps to get the point across, though.
EDIT: The matter with Ciudadanos was similar to that of PP, because of their right-wing ideology. However, they have been rendered irrelevant after the second vote, with only 10 seats.