Kerala to Hampi or vice versa essentially involves multiple connections.
There are no direct connections from any of the major towns in Kerala to Hospet, which is the nearest railway station for Hampi.
By far train is the best possible option to travel to Hampi from Kerala. Though Hospet is the nearest railway station for Hampi, there are no direct trains (not yet at the time of writing this!) from Kerala that travel via Hospet.
But you have two broad choices. Travel to Goa and then travel to Hospet (and Hampi). Or travel to Guntakal Jn and then look for a connection to Hospet.
Of the two there are some half a dozen trains connect Kerala with Goa along the Konkan coast.
To Guntakal there are only two trains (the Mumbai Express Train No:16332; and the daily Kanyakumari Mumbai Jayanti Express Train No:16382 ).
From Goa to Hampi opt for a train connection to Hospet ( Howrah Express , Train No:18048 ) or even there are overnight buses from Goa to Hospet.
While this may not be the shortest, you may also consider a third train option, that is via Bangalore. You can catch that overnight Hampi Express (Train number 16592) from Bangalore to Hospet.
By there way there is a 12 hour or so bus (Rajahamsa service by KSRTC ) connection from Mangalore to Hopset. If you are traveling from the northern part of Kerala, explore the feasibility of this option too.
See also other travel options to reach Hampi.
Cauvery Express or Kaveri Express ( Train No: 6222 / 6221 ) is a long distance train that links the capitals ( Bengaluru and Chennai ) of two south Indian States, Karnataka and Tamilnadu. This No. 6222 express train starts from Chennai Central and terminates at Mysore in Karnataka (via Bangalore) . The down train No 6221 travels in the reverse direction. The pair operates daily. Usually the trains are packed, especially during the weekends . So an advanced reservation is highly desirable, as the seat / berth availability is an issue if not reserved well in advance
Amaravati Express (Train No.18047 / 18048 ) is a cross-country train that connects east India to the west. That is, Goa (Vasco-da-Gama) in the west coast with Howrah Junction in the east.
This train travels via Hospet Junction, the nearest railway station for Hampi. Those coming from the eastern parts of India to Hampi, this is a suitable train. The train covers its 2155 kilometers (1340 miles) journey in 2 days.