Why did Yehudah and Bas Shu'a give their sons such derogatory names ('Er' - childless, and 'Onan' - mourner)?
Targum Yonasan: Because Er would indeed die without children and Onan would cause his father to mourn for him. 1
Pesikta Zutresa (2): Yehudah was stricken due to the sale of Yosef through his wife and children. Their names hint to the sale. Er - [Yosef] was Hu'arah (emptied) from the world. Onan - he brought Aninus (mourning, to his father) over him. Shelah - (Yehudah) erred. He was in Keziv - he lied about the sale.
Malbim: Bas Shu'a had not converted before they were born, and her sons' end was bad.
And Chazal say that when parents name their children, they are inspired by a spark of Ru'ach ha'Kodesh.
Hadar Zekenim (4): It was customary for the father to name the first son, and the mother, the second.
Oznayim la'Torah: It suddenly struck him how he was going from one bad situation to another - (a) He advised the brothers to sell Yosef; (b) He was removed from his position of leadership (and lost his money); (c) He moved away from his brothers and his father's house, and (d) He married a Kena'anis and had a son from her.